package com.ruoyi.chat.controller;

import java.util.List;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.chat.domain.ChatGroup;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.chat.domain.ChatUser;
import com.ruoyi.chat.service.IChatUserService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 聊天用户Controller
 *

 * @date 2023-07-23
 */
@RestController
@RequestMapping("/user")
public class ChatUserController extends BaseController {
    @Autowired
    private IChatUserService chatUserService;

    /**
     * 查询聊天用户列表
     */
    @RequiresPermissions("chat:user:list")
    @GetMapping("/list")
    public TableDataInfo list(ChatUser chatUser) {
        startPage();
        List<ChatUser> list = chatUserService.selectChatUserList(chatUser);
        return getDataTable(list);
    }


    /**
     * 查询用户群列表
     */
    @RequiresPermissions("chat:user:list")
    @GetMapping("/groupList/{userId}")
    public AjaxResult getAllGroup(@PathVariable("userId") Long userId)
    {
        return success(chatUserService.selectChatGroupList(userId));
    }

    /**
     * 查询用户群状态
     */
    @RequiresPermissions("chat:user:list")
    @GetMapping("/userGroupList/{userId}")
    public AjaxResult getUserGroupStatus(@PathVariable("userId") Long userId)
    {
        return success(chatUserService.selectUserGroupStatus(userId));
    }


    /**
     * 导出聊天用户列表
     */
    @RequiresPermissions("chat:user:export")
    @Log(title = "聊天用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChatUser chatUser) {
        List<ChatUser> list = chatUserService.selectChatUserList(chatUser);
        ExcelUtil<ChatUser> util = new ExcelUtil<ChatUser>(ChatUser.class);
        util.exportExcel(response, list, "聊天用户数据");
    }

    /**
     * 获取聊天用户详细信息
     */
    @RequiresPermissions("chat:user:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(chatUserService.selectChatUserById(id));
    }

    /**
     * 根据系统id获取聊天用户详细信息
     */
    @RequiresPermissions("chat:user:query")
    @GetMapping("/getUserBySystemId")
    public AjaxResult getUserBySystemId(Long systemId) {
        return success(chatUserService.getUserBySystemId(systemId));
    }


    /**
     * 获取聊天用户好友信息
     */
    @RequiresPermissions("chat:user:query")
    @GetMapping(value = "findByFriend")
    public AjaxResult getAllInfo(Long id ,Integer friendStatus) {
        return success(chatUserService.selectChatAllUserById(id,friendStatus));
    }

    /**
     * 查询聊天数据（按条数查不重复好友id的最后一个的聊天数据）
     * num 获取的条数
     */
    @RequiresPermissions("chat:user:list")
    @GetMapping("/getAllchatLastOne")
    public AjaxResult getAllchatLastOne(Long userId, Integer num) {
        return success(chatUserService.selectAllchatLastOne(userId, num));
    }

    /**
     *查询单个好友的聊天数据
     */
    @RequiresPermissions("chat:user:list")
    @GetMapping("/getFriendMessage")
    public AjaxResult getFriendMessage(Long userId, Long friendId,Integer page, Integer size) {
        return success(chatUserService.selectFriendMessage(userId, friendId,page,size));
    }


    /**
     * 新增聊天用户
     */
    @RequiresPermissions("chat:user:add")
    @Log(title = "聊天用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChatUser chatUser) {
        return toAjax(chatUserService.insertChatUser(chatUser));
    }

    /**
     * 添加好友
     */
//    @RequiresPermissions("chat:user:edit")
//    @Log(title = "好友添加",businessType = BusinessType.UPDATE)
    @GetMapping("/addFriend")
    public AjaxResult addFriend(Long userId,Long friendId,String messageAdd){

        return toAjax(chatUserService.addFriend(userId,friendId,messageAdd));
    }

    /**
     * 修改好友关系
     * @param userId
     * @param friendId
     * @param friendStatus
     * @return
     */
    @GetMapping("/modifyFriendUser")
    public AjaxResult modifyFriendUser(Long userId,Long friendId,Integer friendStatus){
        return toAjax(chatUserService.updateFriendUser(userId,friendId,friendStatus));
    }

    /**
     * 修改聊天用户
     */
    @RequiresPermissions("chat:user:edit")
    @Log(title = "聊天用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChatUser chatUser) {
        return toAjax(chatUserService.updateChatUser(chatUser));
    }

    /**
     * 删除聊天用户
     */
    @RequiresPermissions("chat:user:remove")
    @Log(title = "聊天用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(chatUserService.deleteChatUserByIds(ids));
    }


    /**
     * 删除聊天用户
     */
    @RequiresPermissions("chat:user:delete")
    @Log(title = "聊天用户", businessType = BusinessType.DELETE)
    @GetMapping("delete")
    public AjaxResult delete(Long id, Long userId) {
        return toAjax(chatUserService.deleteUserById(id, userId));
    }

    /**
     * 添加群用户
     */

    @GetMapping("/addGroupUser")
    public AjaxResult addGroupUser(Long userId,Long groupId){

        Integer groupStatus = 3;
        return toAjax(chatUserService.addGroupUser(userId,groupId,groupStatus));
    }


    /**
     * 根据群主查询申请群的用户
     */

    @GetMapping("/groupUserList")
    public AjaxResult groupUserList(Long groupId){
        System.out.println(1234);
        System.out.println(chatUserService.groupUserList(groupId));
        return success(chatUserService.groupUserList(groupId));
    }

    /**
     * 群主是否同意用户加入群聊
     */

    @GetMapping("/updateGroupUsers")
    public AjaxResult updateGroupUsers(Long groupId,Long userId,Integer groupStatus){

        System.out.println(chatUserService.updateGroupUsers(groupId,userId,groupStatus));
        return toAjax(chatUserService.updateGroupUsers(groupId,userId,groupStatus));
    }
}
